package marchsoft.modules.selfFish.finance.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import marchsoft.modules.selfFish.finance.entity.Cost;
import marchsoft.modules.selfFish.finance.entity.vo.CostVo;
import marchsoft.modules.selfFish.finance.mapper.CostMapper;
import marchsoft.modules.selfFish.finance.service.CostService;
import marchsoft.response.Result;
import marchsoft.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 *@title CostServiceImpl
 *@description
 *@author Lenovo
 *@version 1.0
 *@create 2024-07-15 17:32
 */
@Service
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {


    @Autowired
    private CostMapper costMapper;

    @Override
    public Result<Object> getCostList(String zhName, Long pageNum, Long pageSize) {
        return Result.success(costMapper.getCostList(new Page<>(pageNum, pageSize),zhName));
    }

    @Override
    public Result<Object> getCostTotal() {
        return Result.success(costMapper.getCostTotal());
    }

    @Override
    public void download(String zhName, HttpServletResponse response) throws IOException {
        Page page = new Page<>(-1, -1);
        List<CostVo> records = costMapper.getCostList(page, zhName).getRecords();
        List<Map<String, Object>> list = new ArrayList<>();
        records.forEach(costVo -> {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("物品名称", costVo.getZhName());
            map.put("数量", costVo.getNum());
            map.put("金额", costVo.getPrice());
            map.put("描述", costVo.getDescription());
            map.put("创建时间", LocalDateTimeUtil.format(costVo.getCreatedTime(), DatePattern.NORM_DATETIME_FORMATTER));
            list.add(map);
        });
        Map<Integer, Integer>widthMap = new HashMap<>();
        FileUtils.downloadExcel(list,widthMap, response);
    }
}
